package net.towee.client.com.stuff.presenter;

import com.google.common.base.Preconditions;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.inject.Inject;

import net.towee.client.com.stuff.GroupInformationShower;
import net.towee.client.com.stuff.GroupRequestsShower;
import net.towee.client.com.stuff.GroupRolesShower;
import net.towee.client.com.stuff.GroupShower;
import net.towee.client.com.stuff.GroupUsersShower;
import net.towee.client.managers.GroupsManager;
import net.towee.model.group.GroupIdentity;
import net.towee.model.group.GroupProfile;

public class GroupShowerPresenter implements GroupShower {

	public interface Display extends IsWidget {
		void setGroupName(String name);

		HasWidgets getGroupInformationContainer();

		HasWidgets getRolesContainer();

		HasWidgets getUsersContainer();

		HasWidgets getRequestsContainer();
		
		void showInfo();
	}

	// Injected
	private final Display display;	
	private final GroupInformationShower info;
	private final GroupRolesShower roles;
	private final GroupUsersShower users;
	private final GroupRequestsShower requests;
	
	private GroupProfile group;

	@Inject
	public GroupShowerPresenter(Display display, GroupInformationShower info, GroupRolesShower roles,
			GroupUsersShower users, GroupRequestsShower requests) {
		this.display = display;
		this.info = info;
		this.roles = roles;
		this.users = users;
		this.requests = requests;
	}

	@Override
	public void go(HasWidgets container) {
		Preconditions.checkNotNull(group, "null group identity");
		container.clear();
		container.add(display.asWidget());
		
		info.go( display.getGroupInformationContainer() );
		users.go( display.getUsersContainer() );
		roles.go( display.getRolesContainer() );
		display.showInfo();
	}

	@Override
	public void setGroupProfile(GroupProfile id) {
		this.group = id;
		display.setGroupName(id.getIdentity().getPrintName());
		
		// Set group in editors
		info.setGroupProfile( id );
		roles.setGroupProfile( id );
		users.setGroupProfile( id );
		requests.setGroupProfile( id );
	}

}
